package com.yunwaikeji.gy_tool_library.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.yunwaikeji.gy_tool_library.info.GYBaseInfo;

import java.io.Serializable;

/**
 * SharedPreferences存储工具
 */
public class GYSPUtils implements GYBaseInfo {
    private Gson gson;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor sharedPreferencesEditor;

    private static GYSPUtils gyspUtils;

    public static GYSPUtils getInstance() {
        if (gyspUtils == null) {
            gyspUtils = new GYSPUtils();
        }
        return gyspUtils;
    }

    public void init(Context context, String name) {
        if (gson == null) {
            gson = new Gson();
        }
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
            if (sharedPreferencesEditor == null) {
                sharedPreferencesEditor = sharedPreferences.edit();
            }
        }
    }

    public GYSPUtils set(String key, Object object) {
        if (object == null) {
            Log.e("GYSPUtils", "object为空");
            return this;
        }
        // 得到object的类型
        String type = object.getClass().getSimpleName();
        if ("String".equals(type)) {
            // 保存String 类型
            sharedPreferencesEditor.putString(key, (String) object);
        } else if ("Integer".equals(type) || "int".equals(type)) {
            // 保存integer 类型
            sharedPreferencesEditor.putInt(key, (Integer) object);
        } else if ("Boolean".equals(type) || "boolean".equals(type)) {
            // 保存 boolean 类型
            sharedPreferencesEditor.putBoolean(key, (Boolean) object);
        } else if ("Float".equals(type) || "float".equals(type)) {
            // 保存float类型
            sharedPreferencesEditor.putFloat(key, (Float) object);
        } else if ("Long".equals(type) || "long".equals(type)) {
            // 保存long类型
            sharedPreferencesEditor.putLong(key, (Long) object);
        } else {
            if (!(object instanceof Serializable)) {
                throw new IllegalArgumentException(object.getClass().getName() + " 必须实现Serializable接口!");
            }
            String json = gson.toJson(object);
            sharedPreferencesEditor.putString(key, json);
        }
        sharedPreferencesEditor.commit();
        return this;
    }

    public Object get(String key, Class typeClass) {
        if (typeClass == null) {
            return "";
        }
        if (sharedPreferences == null) {
            Log.e("GYSPUtils", NO_INIT + "sharedPreferences");
            return new Exception(NO_INIT + "sharedPreferences");
        }
        String type = typeClass.getSimpleName();
        if ("String".equals(type)) {
            String s = sharedPreferences.getString(key, "");
            return TextUtils.isEmpty(s) ? "" : s;
        } else if ("Integer".equals(type) || "int".equals(type)) {
            return sharedPreferences.getInt(key, 0);
        } else if ("Boolean".equals(type) || "boolean".equals(type)) {
            return sharedPreferences.getBoolean(key, false);
        } else if ("Float".equals(type) || "float".equals(type)) {
            return sharedPreferences.getFloat(key, 0);
        } else if ("Long".equals(type) || "long".equals(type)) {
            return sharedPreferences.getLong(key, 0);
        } else {
            String json = sharedPreferences.getString(key, "");
            if (!TextUtils.isEmpty(json)) {
                return gson.fromJson(json, typeClass);
            } else {
                return null;
            }
        }
    }

    /**
     * 移除信息
     *
     * @param key
     */
    public void remove(String key) {
        sharedPreferencesEditor.remove(key);
        sharedPreferencesEditor.commit();
    }

    /**
     * 清空信息
     */
    public void clear() {
        sharedPreferencesEditor.clear();
        sharedPreferencesEditor.commit();
    }
}
